/**   
 * @Title: ErrorCodeUtil.java 
 * @author zengshuai shi   
 * @date 2015年8月5日 上午10:00:20 
 */
package com.shanghaionstar.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.AppRUtils;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragmentActivity;
import com.onstar.cn.mag.enumtype.RemoteCommandsType;
import com.onstar.cn.vehicle.entity.ErrorEnum;
import com.onstar.cn.vehicle.entity.ErrorInfo;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.geofence.GeofenceInfoActivity;

import org.apache.http.conn.ConnectTimeoutException;

import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @ClassName: ErrorCodeUtil
 * @Description: TODO
 * @author zengshuai shi
 * @date 2015年8月5日 上午10:00:20
 */
public class ErrorCodeUtil {
	private static LogManager logger = LogManager.getInstance();
	private static DialogInterface.OnClickListener onClickListener;

	public static void ShowErrorSompMsg(Context context, ErrorInfo errorInfo) {
		if (errorInfo != null) {
			logger.d("===>SOMP error Code = " + errorInfo.getCode());
		}
		ShowErrorSompMsg(context, errorInfo, onClickListener);
	}

	public static void ShowErrorSompMsg(Context context, ErrorInfo errorInfo,
			DialogInterface.OnClickListener onClickListener) {
		if (context == null || errorInfo == null) {
			return;
		}
		ErrorEnum error = ErrorEnum.fromValue(errorInfo.getCode());

		if (error == null) {
			AlertDialogUtil.showAlertDialog(context, context.getString(R.string.dialog_warning), errorInfo.getMsg()
					+ " (" + errorInfo.getCode().replaceAll("SOMP-", "1").replaceAll("ONS-", "0") + ")");
			return;
		}
		String errorMsg = getErrorMsgByCode(context, errorInfo);
		switch (error) {
		case SOMP_311:
		case SOMP_315:
		case SOMP_316:
		case SOMP_323:
		case SOMP_503:
			// AlertDialogUtil.showExitDialogForce(context, errorMsg);
			return;
		case ONS_101:
		default:
			break;
		}
		AlertDialogUtil.showAlertDialog(context, context.getString(R.string.dialog_warning), errorMsg, onClickListener);

	}

	public static void ShowErrorOnsMsg(Context context, ErrorInfo errorInfo, RemoteCommandsType requestType) {

		logger.d("===>ONS error Code = " + errorInfo.getCode() + "  RequestType = " + requestType);

		ErrorEnum error = ErrorEnum.fromValue(errorInfo.getCode());
		if (error == null) {

			AlertDialogUtil.showAlertDialog(context, context.getString(R.string.dialog_warning), errorInfo.getMsg()
					+ " (" + errorInfo.getCode().replaceAll("SOMP-", "1").replaceAll("ONS-", "0") + ")");
			return;
		}
		switch (error) {

		case ONS_101:
			// AlertDialogUtil.showExitDialogForce(context, R.string.ONS_101);
			break;
		case ONS_104:
			AlertDialogUtil.showAlertDialog(context, R.string.dialog_warning, R.string.ONS_104);
			break;
		case ONS_105:
			AlertDialogUtil.showAlertDialog(context, R.string.dialog_warning, R.string.ONS_105);
			break;
		case ONS_114:
			AlertDialogUtil.showAlertDialog(context, R.string.dialog_warning, R.string.ONS_114);
			break;
		case ONS_211:
			AlertDialogUtil.showAlertDialog(context, R.string.dialog_warning, R.string.ONS_211);
			break;
		case ONS_218:
			AlertDialogUtil.showAlertDialog(context, R.string.dialog_warning, R.string.ONS_218);
			break;
		case ONS_300:
			if (requestType == null) {
				AlertDialogUtil.showAlertDialog(context, R.string.dialog_warning, R.string.ONS_900);
				// VehicleConstants.pollingRequestStatuses.getRequestTypes()
				// .clear();
			} else {
				if (requestType == RemoteCommandsType.COMMANDS_LOCATION) {
					showMessage(context, requestType, errorInfo);
				} else if (requestType == RemoteCommandsType.COMMANDS_TBT_ROUTE) {
					AlertDialogUtil.showAlertDialog(context, context.getString(R.string.poi_destination_title_str),
							context.getString(R.string.ONS_300));
				} else {
					AlertDialogUtil.showAlertDialog(context, context.getString(R.string.remote_control),
							context.getString(R.string.ONS_300));
				}
			}
			break;

		case SOMP_906:
			AlertDialogUtil.showAlertDialog(context, R.string.dialog_warning, R.string.get_condition_error);
			break;

		default:
			if (requestType == null) {
				// List<RequestType> list =
				// VehicleConstants.pollingRequestStatuses
				// .getRequestTypes();
				// int listSize = list.size();
				// for (int i = 0; i < listSize; i++) {
				// showMessage(context, list.get(i), errorInfo);
				// }
				// VehicleConstants.pollingRequestStatuses.getRequestTypes()
				// .clear();
			} else {
				showMessage(context, requestType, errorInfo);
			}
			break;
		}
	}

	public static void showMessage(Context context, RemoteCommandsType requestType, ErrorInfo errorInfo) {
		try {
			// if (((MainTabActivity) context).CurrentTabIsMainHome()) {
			// return;
			// }
		} catch (Exception e) {
		}
		switch (requestType) {
		case COMMANDS_LOCATION:
			AlertDialogUtil.showAlertDialog(context, context.getString(R.string.category_condition),
					context.getString(R.string.get_condition_error) + "("
							+ errorInfo.getCode().replaceAll("ONS-", "0").replaceAll("SOMP-", "1") + ")");
			break;
		default:
			AlertDialogUtil.showResultAlertDialog(context, false, requestType, errorInfo);
			break;
		}

	}

	public static void setDialogListener(DialogInterface.OnClickListener clickListener) {
		onClickListener = clickListener;
	}

	public static String getErrorMsgByCode(Context context, Exception e) {
		ErrorInfo errorInfo = ErrorCodeUtil.getErrorInfoByException(context, e);
		// ErrorInfo errorInfo=new ErrorInfo();
		// errorInfo.setCode(errorInfo);
		// errorInfo.setMsg(context.getString(R.string.SOMP_904));
		return getErrorMsgByCode(context, errorInfo);
	}

	public static ErrorInfo getErrorInfoByException(Context context, Exception e) {
		if (context == null)
			return null;
		ErrorInfo errorInfo = new ErrorInfo();
		if (e instanceof UnknownHostException) {// 网络问题
			errorInfo.setCode(ErrorEnum.SOMP_901.getStringValue());
			errorInfo.setMsg(context.getString(R.string.networkError));
		} else if (e instanceof ConnectTimeoutException || e instanceof SocketTimeoutException) {// 超时
			errorInfo.setCode(ErrorEnum.SOMP_902.getStringValue());
			errorInfo.setMsg(context.getString(R.string.networkError));
		} else {
			errorInfo.setCode(ErrorEnum.SOMP_904.getStringValue());
			errorInfo.setMsg(context.getString(R.string.networkError));
		}

		return errorInfo;
	}


	public static ErrorInfo getErrorInfoByHttpException(Context context, HttpException e) {
		ErrorInfo errorInfo = new ErrorInfo();
		if (!CommonUtil.isNull(e.getErrorCode())) {
			try{
//				if(e.getErrorCode().contains("L7_")) {
//					int id = AppRUtils.getInstance(context).getResouceId("string", e.getErrorCode());
//					String errorMsg = context.getString(id);
//					errorInfo.setMsg(errorMsg);
//					return errorInfo;
//				}
				if(e.getErrorCode().contains("ONS")) {
					String errorCode = e.getErrorCode().replace("-", "_");
					int id = AppRUtils.getInstance(context).getResouceId("string", errorCode);
					String errorMsg = context.getString(id);
					errorInfo.setMsg(errorMsg);
					return errorInfo;
				}
			}catch(Exception e1){
				errorInfo = ErrorCodeUtil.getErrorInfoByException(context, e1);
			}
			if(CommonUtil.isNull(e.getMessage())) {
				int id = AppRUtils.getInstance(context).getResouceId("string", e.getErrorCode());
				String errorMsg = context.getString(id);
				errorInfo.setMsg(errorMsg);
			}else if(e.getErrorCode().contains("L7_")){
				if(e.getErrorCode().equals("L7_invalid_user_credentials_dummy") || e.getErrorCode().equals("L7_invalid_user_credentials_number_dummy")) {//针对note2等特殊机型登录或是pin码验证返回不识别
					int id = AppRUtils.getInstance(context).getResouceId("string", e.getErrorCode());
					String errorMsg = context.getString(id);
					errorInfo.setMsg(errorMsg);
					return errorInfo;
				}
				String[] errorMsgs = e.getMessage().split(":");
				if(errorMsgs.length == 2) {
					String numberInMsg = errorMsgs[1];
					int id = 0;
					if(context instanceof BaseFragmentActivity) {
						BaseFragmentActivity activity = (BaseFragmentActivity) context;
//						if(activity.getCurrentFragmentTag().contains("pin_dialog")) {//pinfragment/mropinfragment
						List<Fragment> fragments = activity.getSupportFragmentManager().getFragments();
						boolean hasPin = false;
						if(fragments!=null && fragments.size() >0) {
							for(Fragment fragment : fragments) {
								if(fragment.getClass().getSimpleName().contains("PinDialogFragment")) {
									hasPin = true;
								}
							}
						}
						if (activity.getSupportFragmentManager().findFragmentByTag("pin_dialog") != null || hasPin) {//pinfragment/mropinfragment
							id = AppRUtils.getInstance(context).getResouceId("string", e.getErrorCode() + "_number");
						} else {
							id = AppRUtils.getInstance(context).getResouceId("string", e.getErrorCode());
						}
					}else if(context instanceof GeofenceInfoActivity){
						BaseActivity activity = (BaseActivity) context;
						if (activity.getSupportFragmentManager().findFragmentByTag("pin_dialog") != null) {//pinfragment/mropinfragment
							id = AppRUtils.getInstance(context).getResouceId("string", e.getErrorCode() + "_number");
						} else {
							id = AppRUtils.getInstance(context).getResouceId("string", e.getErrorCode());
						}
					}else {//loginactivity
						id = AppRUtils.getInstance(context).getResouceId("string", e.getErrorCode());
					}
					if(id == 0) {
						errorInfo = ErrorCodeUtil.getErrorInfoByException(context, e);
					}else {
						String errorMsg = context.getString(id,numberInMsg);
						errorInfo.setMsg(errorMsg);
					}
				}else {
					errorInfo = ErrorCodeUtil.getErrorInfoByException(context, e);
				}
			}else {
				errorInfo.setCode(e.getErrorCode());
				errorInfo.setMsg(e.getMessage());
			}
		} else {
			errorInfo = ErrorCodeUtil.getErrorInfoByException(context, e);
		}
		return errorInfo;
	}

	public static String getErrorMsgByCode(Context context, ErrorInfo errorInfo) {
		if (errorInfo == null) {
			errorInfo = new ErrorInfo();
			errorInfo.setCode(ErrorEnum.SOMP_904.getStringValue());
		}
		if (errorInfo.getCode() == null) {
			errorInfo = new ErrorInfo();
			errorInfo.setCode(ErrorEnum.SOMP_904.getStringValue());
		}

		if (context == null) {
			return null;
		}
		ErrorEnum error = ErrorEnum.fromValue(errorInfo.getCode());
		String errorMsg = "";
		if (error == null) {
			return errorInfo.getMsg() + " (" + errorInfo.getCode().replaceAll("SOMP-", "1").replaceAll("ONS-", "0")
					+ ")";
		}
		switch (error) {
		case SOMP_301:
			errorMsg = context.getString(R.string.SOMP_301);
			break;
		case SOMP_302:
			errorMsg = context.getString(R.string.SOMP_302);
			break;
		case SOMP_304:
			errorMsg = context.getString(R.string.SOMP_304);
			break;

		case SOMP_305:
			errorMsg = context.getString(R.string.SOMP_305);
			break;

		case SOMP_307:
			errorMsg = context.getString(R.string.SOMP_307);
			break;

		case SOMP_308:
			errorMsg = context.getString(R.string.SOMP_308);
			break;
		case SOMP_309:
			errorMsg = context.getString(R.string.SOMP_309);
			break;

		case SOMP_310:
			errorMsg = context.getString(R.string.SOMP_310);
			break;

		case SOMP_311:
			errorMsg = context.getString(R.string.SOMP_311);
			break;

		case SOMP_312:
			errorMsg = context.getString(R.string.SOMP_312);
			break;

		case SOMP_313:
			errorMsg = context.getString(R.string.SOMP_313);
			break;

		case SOMP_314:
			errorMsg = context.getString(R.string.SOMP_314);
			break;
		case SOMP_315:
			errorMsg = context.getString(R.string.SOMP_315);
			break;
		case SOMP_316:
			errorMsg = context.getString(R.string.SOMP_316);
			break;

		case SOMP_318:
			errorMsg = context.getString(R.string.SOMP_318);
			break;
		case SOMP_319:
			errorMsg = context.getString(R.string.SOMP_319);
			break;

		case SOMP_323:
			errorMsg = context.getString(R.string.SOMP_323);
			break;
		case SOMP_324:
			errorMsg = context.getString(R.string.SOMP_324);
			break;
		case SOMP_325:
			errorMsg = context.getString(R.string.SOMP_325);
			break;
		case SOMP_401:
			errorMsg = context.getString(R.string.SOMP_401);
			break;
		case SOMP_402:
			errorMsg = context.getString(R.string.SOMP_402);
			break;

		case SOMP_403:
			errorMsg = context.getString(R.string.SOMP_403);
			break;
		case SOMP_404:
			errorMsg = context.getString(R.string.SOMP_404);
			break;
		case SOMP_405:
			errorMsg = context.getString(R.string.SOMP_405);
			break;
		case SOMP_501:
			errorMsg = context.getString(R.string.SOMP_501);
			break;
		case SOMP_503:
			errorMsg = context.getString(R.string.SOMP_503);
			break;
		case SOMP_601:
			errorMsg = context.getString(R.string.SOMP_601);
			break;
		case SOMP_603:
			errorMsg = context.getString(R.string.SOMP_603);
			break;
		case SOMP_604:
			errorMsg = context.getString(R.string.SOMP_604);
			break;
		case SOMP_701:
			errorMsg = context.getString(R.string.SOMP_701);
			break;
		case SOMP_702:
			errorMsg = context.getString(R.string.SOMP_702);
			break;

		case SOMP_901:
			errorMsg = context.getString(R.string.SOMP_901);
			break;
		case SOMP_902:
			errorMsg = context.getString(R.string.SOMP_902);
			break;
		case SOMP_903:
			errorMsg = context.getString(R.string.SOMP_903);
			break;
		case SOMP_904:
			errorMsg = context.getString(R.string.SOMP_904);
			break;

		case ONS_101:
			errorMsg = context.getString(R.string.ONS_101);
			break;
		case ONS_104:
			errorMsg = context.getString(R.string.ONS_104);
			break;
		case ONS_105:
			errorMsg = context.getString(R.string.ONS_105);
			break;
		case ONS_106:
			errorMsg = context.getString(R.string.ONS_106);
			break;
		case ONS_107:
			errorMsg = context.getString(R.string.ONS_107);
			break;
		case ONS_108:
			errorMsg = context.getString(R.string.ONS_108);
			break;
		case ONS_110:
			errorMsg = context.getString(R.string.ONS_110);
			break;
		case ONS_111:
			errorMsg = context.getString(R.string.ONS_111);
			break;
		case ONS_112:
			errorMsg = context.getString(R.string.ONS_112);
			break;
		case ONS_113:
			errorMsg = context.getString(R.string.ONS_113);
			break;
		case ONS_114:
			errorMsg = context.getString(R.string.ONS_114);
			break;
		case ONS_201:
			errorMsg = context.getString(R.string.ONS_201);
			break;
		case ONS_210:
			errorMsg = context.getString(R.string.ONS_210);
			break;
		case ONS_211:
			errorMsg = context.getString(R.string.ONS_211);
			break;
		case ONS_212:
			errorMsg = context.getString(R.string.ONS_212);
			break;
		case ONS_213:
			errorMsg = context.getString(R.string.ONS_213);
			break;
		case ONS_214:
			errorMsg = context.getString(R.string.ONS_214);
			break;
		case ONS_215:
			errorMsg = context.getString(R.string.ONS_215);
			break;
		case ONS_216:
			errorMsg = context.getString(R.string.ONS_216);
			break;
		case ONS_217:
			errorMsg = context.getString(R.string.ONS_217);
			break;
		case ONS_218:
			errorMsg = context.getString(R.string.ONS_218);
			break;
		case ONS_219:
			errorMsg = context.getString(R.string.ONS_219);
			break;
		case ONS_220:
			errorMsg = context.getString(R.string.ONS_220);
			break;
		case ONS_230:
			errorMsg = context.getString(R.string.ONS_230);
			break;
		case ONS_231:
			errorMsg = context.getString(R.string.ONS_231);
			break;
		case ONS_232:
			errorMsg = context.getString(R.string.ONS_232);
			break;
		case ONS_233:
			errorMsg = context.getString(R.string.ONS_233);
			break;
		case ONS_234:
			errorMsg = context.getString(R.string.ONS_234);
			break;
		case ONS_300:
			errorMsg = context.getString(R.string.ONS_300);
			break;
		case ONS_301:
			errorMsg = context.getString(R.string.ONS_301);
			break;
		case ONS_801:
			errorMsg = context.getString(R.string.ONS_801);
			break;
		case ONS_802:
			errorMsg = context.getString(R.string.ONS_802);
			break;
		case ONS_803:
			errorMsg = context.getString(R.string.ONS_803);
			break;
		case ONS_804:
			errorMsg = context.getString(R.string.ONS_804);
			break;
		case ONS_805:
			errorMsg = context.getString(R.string.ONS_805);
			break;
		case ONS_806:
			errorMsg = context.getString(R.string.ONS_806);
			break;
		case ONS_900:
			errorMsg = context.getString(R.string.ONS_900);
			break;
		case ONS_901:
			errorMsg = context.getString(R.string.ONS_901);
			break;
		case ONS_902:
			errorMsg = context.getString(R.string.ONS_902);
			break;
		case ONS_903:
			errorMsg = context.getString(R.string.ONS_903);
			break;
		case ONS_910:
			errorMsg = context.getString(R.string.ONS_910);
			break;

		// 以下为JOIN me 异常信息
		case SOMP_326:
			errorMsg = context.getString(R.string.SOMP_326);
			break;
		case SOMP_327:
			errorMsg = context.getString(R.string.SOMP_327);
			break;
		case SOMP_328:
			errorMsg = context.getString(R.string.SOMP_328);
			break;
		case SOMP_329:
			errorMsg = context.getString(R.string.SOMP_329);
			break;
		case SOMP_330:
			errorMsg = context.getString(R.string.SOMP_330);
			break;
		case SOMP_331:
			errorMsg = context.getString(R.string.SOMP_331);
			break;
		case SOMP_317:
			errorMsg = context.getString(R.string.SOMP_317);
			break;
		case SB021_MSG004:
			errorMsg = context.getString(R.string.SB021_MSG004);
			break;
		case SB021_MSG006:
			errorMsg = context.getString(R.string.SB021_MSG006);
			break;
		case SB021_MSG007:
			errorMsg = context.getString(R.string.SB021_MSG007);
			break;
		case SB021_MSG008:
			errorMsg = context.getString(R.string.SB021_MSG008);
			break;
		case SB021_MSG011:
			errorMsg = context.getString(R.string.SB021_MSG011);
			break;
		case SB021_MSG012:
			errorMsg = context.getString(R.string.SB021_MSG012);
			break;
		case SB021_MSG013:
			errorMsg = context.getString(R.string.SB021_MSG013);
			break;
		case SB021_MSG014:
			errorMsg = context.getString(R.string.SB021_MSG014);
			break;
		case SB021_MSG019:
			errorMsg = context.getString(R.string.SB021_MSG019);
			break;

		case SB021_MSG020:
			errorMsg = context.getString(R.string.SB021_MSG020);
			break;
		case SB021_MSG022:
			errorMsg = context.getString(R.string.SB021_MSG022);
			break;
		case SB021_MSG023:
			errorMsg = context.getString(R.string.SB021_MSG023);
			break;
		case SB021_MSG001:
			errorMsg = context.getString(R.string.SB021_MSG001);
			break;
		case SB021_MSG026:
			errorMsg = context.getString(R.string.SB021_MSG026);
			break;
		case SB021_MSG031:
			errorMsg = context.getString(R.string.SB021_MSG031);
			break;
		case SB021_MSG032:
			errorMsg = context.getString(R.string.SB021_MSG032);
			break;

		// Update userInfo
		case SB022_MSG003:
			errorMsg = context.getString(R.string.SB022_MSG003);
			break;
		case SB022_MSG011:
			errorMsg = context.getString(R.string.SB022_MSG011);
			break;
		case SB022_MSG012:
			errorMsg = context.getString(R.string.SB022_MSG012);
			break;
		case SB022_MSG013:
			errorMsg = context.getString(R.string.SB022_MSG013);
			break;
		case SB022_MSG015:
			errorMsg = context.getString(R.string.SB022_MSG015);
			break;
		case SB022_MSG016:
			errorMsg = context.getString(R.string.SB022_MSG016);
			break;
		case SB021_28:

		case SB022_MSG017:
			errorMsg = context.getString(R.string.SB022_MSG017);
			break;
		case SBMAG_MSG001:
			errorMsg = context.getString(R.string.SBMAG_MSG001);
			break;
		case SB024_MSG002:
			errorMsg = context.getString(R.string.SB024_MSG002);
			break;

		case SB027_MSG001:
			errorMsg = context.getString(R.string.SB027_MSG001);
			break;
		case SB027_MSG002:
			errorMsg = context.getString(R.string.SB027_MSG002);
			break;
		case SB027_MSG003:
			errorMsg = context.getString(R.string.SB027_MSG003);
			break;
		case SB027_MSG004:
			errorMsg = context.getString(R.string.SB027_MSG004);
			break;
		case SB027_MSG005:
			errorMsg = context.getString(R.string.SB027_MSG005);
			break;
		case SB027_MSG006:
			errorMsg = context.getString(R.string.SB027_MSG006);
			break;
		case SB027_MSG007:
			errorMsg = context.getString(R.string.SB027_MSG007);
			break;
		case SB027_MSG008:
			errorMsg = context.getString(R.string.SB027_MSG008);
			break;
		case SB027_MSG009:
			errorMsg = context.getString(R.string.SB027_MSG009);
			break;
		case SB027_MSG020:
			errorMsg = context.getString(R.string.SB027_MSG020);
			break;
		case SB006_MSG002:
			errorMsg = context.getString(R.string.SB006_MSG002);
			break;
		default:
			errorMsg = errorInfo.getMsg() + " (" + errorInfo.getCode().replaceAll("SOMP-", "1").replaceAll("ONS-", "0")
					+ ")";
			break;
		}
		return errorMsg;
	}
}
